package com.ydw;

import com.alibaba.fastjson.JSONObject;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author Yangdw
 * @date 2025-09-01
 * @description:
 */
public class Producer {

    public static void main(String[] args) throws MQClientException, MQBrokerException, RemotingException, InterruptedException {
        DefaultMQProducer producer = new DefaultMQProducer("test-delay-group",true);

        producer.setNamesrvAddr("127.0.0.1:9876");

        producer.start();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        for (int i = 0; i < 3; i++) {
            JSONObject data = new JSONObject();
            data.put("date", LocalDateTime.now());
            data.put("message","你好好");
            data.put("datetime",dateTimeFormatter.format(LocalDateTime.now()));
            Message message = new Message(
                    "custom-delay-topic",
                    "delayTag",
                    data.toJSONString().getBytes(StandardCharsets.UTF_8)
            );
            message.setDelayTimeLevel(7);
            producer.send(message);
        }

    }
}
